
package dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.text.SimpleDateFormat;
import java.util.Date;

import DBM.DBManager;

public class LogDAO {
	private final String	logLoginAction	= "INSERT INTO Log(accountID, accepted, ip, time, userAgent) VALUES(?, ?, ?, ?, ?)";
	private DBManager		dbmanager		= null;

	public LogDAO( DBManager DBM ) {
		this.dbmanager = DBM;
	}

	public void logLoginAction( int accountID, boolean accepted,
			String remoteAddr, Date date, String userAgent ) {
		String inputDate = getStringFromCalendar( date );
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( logLoginAction );
			ppstm.setInt( 1, accountID );
			ppstm.setBoolean( 2, accepted );
			ppstm.setString( 3, remoteAddr );
			ppstm.setString( 4, inputDate );
			ppstm.setString( 5, userAgent );
			ppstm.execute();
		} catch( SQLIntegrityConstraintViolationException e ) {
			e.printStackTrace();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	private String getStringFromCalendar( Date time ) {
		if( time == null )
			return "";
		SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		return sdf.format( time.getTime() );
	}
}
